package edu.hrbu.trade01.interceptor;

import edu.hrbu.trade01.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 徐登宇
 */
@Slf4j
public class LoginIntercepter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取session
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        // 判断是否为空
        if (loginUser!=null) {
            return true;
        }
        response.sendRedirect(request.getContextPath()+"/web/login.html");
        return false;
    }
}
